package org.amse.ys.sndeditor.ui;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.*;
import org.amse.ys.sndeditor.ui.MyView;
import org.amse.ys.sndeditor.ui.R.string;
import org.amse.ys.sndeditor.model.IEditor;
import org.amse.ys.sndeditor.model.Localiser; //import org.amse.ys.sndeditor.model.drawer.AndroidDrawer;
import org.amse.ys.sndeditor.model.drawer.AndroidDrawer;
import org.amse.ys.sndeditor.model.drawer.RequestBoolean;
import org.amse.ys.sndeditor.model.editor.ImplementedEditor;

public class Soundeditor extends Activity {

	private int FIRST_ID_IN_MENU = Menu.FIRST; // number of the first option in
												// submenu
	private int mySubMenuFirst; // number of the first option in submenu
	private IEditor myEditor; // link to model
	private View myView; // link to the area, where each module can draw
	public static String myFileToOpen = ""; // field used to send fileName
	private Menu myMenu;

	// first we show dialog to open file and initilize model
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myEditor = ImplementedEditor.getEditor();
		myView = new MyView(this, myEditor);

		try {
			final String fileToOpen = getIntent().getExtras().getString(
					FileOpener.FILE_NAME);
			if (myEditor.fileIsOpened(fileToOpen)) {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("To reopen?");
				builder.setCancelable(false);
				builder.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								try {
									myEditor.onOpenFile(fileToOpen);
									setContentView(myView);
									// myView.requestFocus();
								} catch (IOException e) {
								}
							}
						});
				builder.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								try {

									myEditor.reopenFile(fileToOpen);
									setContentView(myView);
									myView.requestFocus();
								} catch (IOException e) {
								}
							}
						});
				AlertDialog alert = builder.create();
				alert.show();

			} else {
				myEditor.onOpenFile(fileToOpen);
				setContentView(myView);
				myView.requestFocus();

			}
		} catch (IOException e) {
			myEditor.getDrawer().showMessage(
					org.amse.ys.sndeditor.ui.R.string.cant_open_file
							+ getIntent().getExtras().getString(
									FileOpener.FILE_NAME));
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// creating menu model
	private boolean createModelMenu(Menu menu) {
		menu.clear();
		int numberOfOptions = myEditor.getNumberOfMenuOptions();
		for (int i = 0; i < numberOfOptions; i++) {
			MenuItem item = menu.add(0, FIRST_ID_IN_MENU + i, 0, myEditor
					.getMenuItemName(i));
			int icon = myEditor.getMenuItemIconIndex(i);
			System.out.println("setting icon " + icon);
			if (icon != -1) {
				item.setIcon(icon);
			}
			System.out.println("setting icon done");
		}
		mySubMenuFirst = FIRST_ID_IN_MENU + numberOfOptions;
		SubMenu subMenu = menu.addSubMenu(Localiser.getString(string.options));
		// TODO
		int numberOfApplets = myEditor.getNumberOfApplets();
		for (int i = 0; i < numberOfApplets; i++) {
			subMenu.add(1, i + mySubMenuFirst, 0, myEditor.getAppletName(i));
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		myMenu = menu;
		return createModelMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int menuItemIndex = item.getItemId();
		if ((FIRST_ID_IN_MENU <= menuItemIndex)
				&& (menuItemIndex < mySubMenuFirst)) {
			if (myEditor.onMenuItemSelected(menuItemIndex - FIRST_ID_IN_MENU)) {
				myView.refreshDrawableState();
				return true;
			} else {

				final Activity activityToClose = this;
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Quit withiut saving?");
				builder.setCancelable(false);
				builder.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								activityToClose.finish();
							}
						});
				builder.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							}
						});
				builder.show();

			}
		} else if ((mySubMenuFirst <= menuItemIndex)
				&& (menuItemIndex < mySubMenuFirst
						+ myEditor.getNumberOfApplets())) {
			myEditor.setActiveApplet(menuItemIndex - mySubMenuFirst);
		}
		myView.refreshDrawableState();
		this.onCreateOptionsMenu(myMenu);
		return true;
	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {

		if (event.getX() + event.getY() == 0) {
			myEditor.onDPadKeyPressed();
		} else {
			myEditor.onScroll(event.getX() + event.getY());
		}
		myView.refreshDrawableState();

		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_CENTER:
			myEditor.onDPadKeyPressed();
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			myEditor.onScroll(-0.333);
			break;

		case KeyEvent.KEYCODE_DPAD_RIGHT:
			myEditor.onScroll(0.333);
			break;

		case KeyEvent.KEYCODE_VOLUME_UP:
			myEditor.onMagnify(1.5);
			break;
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			myEditor.onMagnify(0.667);
			break;
		case KeyEvent.KEYCODE_BACK:
			finish();

			/*
			 * case KeyEvent.KEYCODE_CALL: openFile(); return true;
			 */
		default:
			return false;
		}
		myView.refreshDrawableState();

		return true;
	}

	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_VOLUME_UP:
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			return true;
		}
		return false;
	}

}